package com.web.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.web.springcloud.entities.Dept;
import com.web.springcloud.service.impl.DeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DeptController {
    @Autowired
    private DeptServiceImpl service;

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 一旦调用方法失败抛出错误信息，就会自动调用@HystrixCommand的注解，执行制定方法
     * @param id
     * @return
     */
    @RequestMapping(value = "/dept/get",method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "processHystrix_Get")//processHystrix_Get方法名
    public Dept get(@PathVariable("id") Long id){
        Dept dept = service.findById(id);
        if(null == dept){
            throw new RuntimeException("该ID"+id+"没有对应的信息");
        }
        return dept;
    }
    //回调
    public Dept processHystrix_Get(@PathVariable("id") Long id) {
        return new Dept()
                       .setDeptNo(id)
                       .setDeptName("该ID"+id+"没有对应的信息")
                       .setDbSource("no this datebase in mysql");
    }


}
